﻿using System;
using System.Web;
using System.Web.Mvc;
using TidyBlog.MVC;
using TidyBlog.Entity;
using TidyBlog.BLL;

namespace TidyBlog.MVC.WWW.Controllers
{
    /// <summary>
    /// 分类管理控制器 /Manage_Category/{action}
    /// </summary>
    public class Manage_CategoryController : ManageController
    {
        /// <summary>
        /// 分类列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View(CategoryBLL.List(0));
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id">分类编号</param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            CategoryBLL.Delete(id);

            RespondentInfo.Success(this.ControllerContext, new OperInfo()
            {
                Message = "已删除指定分类",
                Link = Url.Action("List")
            });

            return new EmptyResult();
        }

        /// <summary>
        /// 创建分类界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }
        
        /// <summary>
        /// 创建分类
        /// </summary>
        /// <param name="category">分类</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(Category category)
        {
            CategoryBLL.Create(category);

            RespondentInfo.Success(this.ControllerContext, new OperInfo
            {
                Message = "已创建分类 " + category.CategoryName,
                Link = Url.Action("List"),
                LinkText = "返回列表"
            });

            return new EmptyResult();
        }

        /// <summary>
        /// 编辑分类界面
        /// </summary>
        /// <param name="id">分类编号</param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var category = CategoryBLL.Read(id);

            if (category == null)
            {
                throw new Exception("分类不存在");
            }

            return View(category);
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Category category)
        {
            CategoryBLL.Update(category);

            RespondentInfo.Success(this.ControllerContext, new OperInfo()
            {
                Message = "已更新分类 " + category.CategoryName,
                Link = Url.Action("List"),
                LinkText = "返回列表"
            });

            return new EmptyResult();
        }
    }
}
